<?php
namespace Particle\Addons;
use Particle\Core as Core;

final class errorControlAddons extends Core\Addons {
   
    function __construct() {
        /*setting addons*/
        
        parent::__construct();
        
        $nameAddons= 'errorControl';
        Core\App::getInstance()->setAppCurrentAddons($nameAddons);
        /* end setting addons*/
    }
    
    public function index()
    {
        $this->_viewAddons->assign('title', 'Error');
        $this->_viewAddons->assign('message', $this->_getError());
        $this->_viewAddons->show('index');
    }
    
    public function message($errorKey = false)
    {
        //echo '<pre>';
        //print_r($this->_viewAddons);
        //echo '</pre>';
        $this->_viewAddons->assign('title', 'Error');
        $this->_viewAddons->assign('message', $this->_getError($errorKey));
        // \Particle\Apps\essential::redirect('http://google.com.uy/', true);
        $this->_viewAddons->show('message');
        exit();
    }
    
    private function _getError($errorKey = false)
    {
        if($errorKey){
            $errorKey = Core\Security::filterInt($errorKey);
        }
        else{
            $errorKey = 'default';
        }
        
        $error = array();
        $error['default'] = 'There was an error and the page can not be shown';
        $error['5050'] = 'Restricted access!';
        $error['8080'] = 'Session time expired!';
        $error['9090'] = 'Problems with database connection!';
        
        if(array_key_exists($errorKey, $error)){
            return $error[$errorKey];
        }
        else{
            return $error['default'];
        }
    }
    
}